package com.app.controller.authority;

import com.app.controller.BaseController;
import com.app.entity.Pagination;
import com.app.entity.authority.Dictionary;
import com.app.service.authority.DictionaryService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by lijun on 2018/2/22.
 */
@RestController
@RequestMapping("/api/dictionary")
public class DictionaryController extends BaseController {

	private static final Logger log = LoggerFactory.getLogger(DictionaryController.class);

	@Autowired
	private DictionaryService dictionaryService;

	/**
	 * 查    Dictionary列表
	 * @param page
	 * @return
	 */
	@RequestMapping(value = "/getInfo", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> getInfo(@RequestBody Pagination page) {

		Map<String, Object> result = new HashMap<String, Object>();
		try {
			Pagination p = this.dictionaryService.getInfo(page);

			result.put("code", 200);
			result.put("message", "succ");
			result.put("data", p);
			return result;

		} catch (Exception e) {
			log.error("func:getInfo %n page:" + page + "%n" + e.getMessage());
			result.put("code", 500);
			result.put("message", e);
			return result;
		}
	}

	/**
	 * 查    modify-Dictionary
	 * @param _data
	 * @return
	 */
	@RequestMapping(value = "/getList", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> getList(@RequestBody Dictionary _data) {

		Map<String, Object> result = new HashMap<String, Object>();
		try {
			List<Dictionary> p = this.dictionaryService.getList(_data);
			result.put("code", 200);
			result.put("message", "succ");
			result.put("data", p);
			return result;

		} catch (Exception e) {
			log.error("func:getList %n _data:" + _data + "%n" + e.getMessage());
			result.put("code", 500);
			result.put("message", e);
			return result;
		}
	}

	/**
	 * 增
	 * @param _data
	 * @return
	 */
	@RequestMapping(value = "/insert", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> insert(@RequestBody Dictionary _data) {

		//添加ID
		_data.setId(java.util.UUID.randomUUID().toString());

		Map<String, Object> results = new HashMap<String, Object>();
		try {
			Integer resultcount = this.dictionaryService.insert(_data);

			if (resultcount > 0) {
				results.put("code", 200);
				results.put("message", "操作成功！");
				results.put("data", _data);
			} else {
				results.put("code", 201);
				results.put("message", "角色编码重复，请选择其他角色编号！");
				results.put("data", "-1");
			}
			return results;

		} catch (Exception e) {
			log.error("func:insert %n _data:" + _data + "%n" + e.getMessage());
			results.put("code", 500);
			results.put("message", e.getMessage());
			return results;
		}
	}

	/**
	 * 判断CODE是否已存在
	 * @param _data
	 * @return
	 */
	@RequestMapping(value = "/validateCode", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> validateCode(@RequestBody Dictionary _data) {

		//ID赋值
		if (_data.getId() == null || _data.getId().length() <= 0) {
			_data.setId("");
		}

		Map<String, Object> result = new HashMap<String, Object>();
		try {
			List<Dictionary> list = this.dictionaryService.validateLevelCode(_data);

			if (list.size() > 0) {
				result.put("code", 201);
				result.put("message", "当前种类中已存在该编码！请选择其他编码");
			} else {
				result.put("code", 200);
				result.put("message", "succ");
			}
			return result;

		} catch (Exception e) {
			log.error("func:validateCode %n _data:" + _data + "%n" + e.getMessage());
			result.put("code", 500);
			result.put("message", e);
			return result;
		}
	}

	/**
	 * 改
	 * @param _data
	 * @return
	 */
	@RequestMapping(value = "/updateList", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> updateList(@RequestBody Dictionary _data) {
		Map<String, Object> results = new HashMap<String, Object>();

		if (_data == null) {
			results.put("code", 400);
			results.put("message", "操作失败！请联系管理员！");
			results.put("data", "-1");
			return results;
		}

		try {
			Integer resultcount = this.dictionaryService.updateList(_data);

			if (resultcount > 0) {
				results.put("code", 200);
				results.put("message", "操作成功！");
			} else {
				results.put("code", 201);
				results.put("message", "操作失败！请联系管理员！");
			}
			return results;

		} catch (Exception e) {
			log.error("func:updateList %n _data:" + _data + "%n" + e.getMessage());
			results.put("code", 500);
			results.put("message", e.getMessage());
			return results;
		}
	}

	/**
	 * 删
	 * @param _data
	 * @return
	 */
	@RequestMapping(value = "/delList", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> delList(@RequestBody Dictionary _data) {
		Map<String, Object> results = new HashMap<String, Object>();

		if (_data == null) {
			results.put("code", 400);
			results.put("message", "操作失败！请联系管理员！");
			results.put("data", "-1");
			return results;
		}

		try {
			Integer resultcount = this.dictionaryService.delList(_data);

			if (resultcount > 0) {
				results.put("code", 200);
				results.put("message", "操作成功！");
				results.put("data", _data);
			} else {
				results.put("code", 201);
				results.put("message", "操作失败！请联系管理员！");
				results.put("data", "-1");
			}
			return results;

		} catch (Exception e) {
			log.error("func:delList %n _data:" + _data + "%n" + e.getMessage());
			results.put("code", 500);
			results.put("message", e.getMessage());
			return results;
		}
	}

}
